package com.kfm.base.thread.day02;


public class Demo06 {

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                Lazy.getInstance();
            }).start();
        }
    }
}

class Lazy {
    private Lazy() {
        System.out.println("init<>");
    }

    private static Lazy INSTANCE;

    // DCL
    public static Lazy getInstance() {
        if (INSTANCE == null) {
            synchronized (Lazy.class) {
                if (INSTANCE == null) {
                    INSTANCE = new Lazy(); // new 操作不是 原子 操作
                }
            }
        }
        return INSTANCE;
    }

}

class Singleton {

    private Singleton() {
        System.out.println("init<>");
    }

    public static class Inner {
        private static final Singleton INSTANCE = new Singleton();
    }

    public static Singleton getInstance() {
        return Inner.INSTANCE;
    }
}
